package org.xserver.bootstrap.listener;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xserver.bootstrap.listener.exception.BootstrapListenerError;
import org.xserver.component.log.StdOutLog;

public class BootstrapListenerManager {
	private static final Logger log = LoggerFactory
			.getLogger(BootstrapListenerManager.class);
	private static final Logger stdOutLog = StdOutLog.getLogger();

	private List<BootstrapListener> bootstrapListeners;

	public List<BootstrapListener> getBootstrapListeners() {
		return bootstrapListeners;
	}

	public void setBootstrapListeners(List<BootstrapListener> bootstrapListeners) {
		this.bootstrapListeners = bootstrapListeners;
	}

	public void process() throws Exception {
		if (null == bootstrapListeners) 
			return;
		
		for (BootstrapListener bootstrapListener : bootstrapListeners) {
			try {
				bootstrapListener.process();
			} catch (BootstrapListenerError e) {
				if (bootstrapListener.isExceptionContinue()) {
					log.error("Do the listener[" + bootstrapListener.getName()
							+ "] failure.", e);
					stdOutLog.error("Do the listener[" + bootstrapListener.getName()
							+ "] failure.", e);
					continue;
				} else {
					throw e;
				}
			}

		}
	}
}
